package com.lzc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzc.mapper.CommentMapper;
import com.lzc.mapper.ErrorKeyMapper;
import com.lzc.pojo.dto.CommentDto;
import com.lzc.pojo.entity.Article;
import com.lzc.pojo.entity.Comment;
import com.lzc.pojo.entity.ErrorKey;
import com.lzc.pojo.vo.CommentPutVo;
import com.lzc.pojo.vo.Result;
import com.lzc.service.CommentService;
import com.lzc.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ErrorKeyMapper errorKeyMapper;

    /**
     * 查找所有评论 并且按父子关系整理好
     * @param articleId 文章id
     * @param page 分页
     * @param size 每页数量
     * @return
     */
    public Result findAllComments(Long articleId,Integer page,Integer size){
        // 获取所有评论
        List<CommentDto> sources = commentMapper.findArticleComments(articleId, page, size);
        // 存放顶级评论
        List<CommentDto> comments = new ArrayList<>();
        for (CommentDto element : sources) {
            // 找到顶级评论
            if (element.getRootCommentId() == null){
                // 存入集合   recursion() 找到传入element的子评论
                comments.add(recursion(element,sources));
            }
        }
        return Result.OK(comments);
    }

    /**
     * 查找当前文章的所有评论的数量
     * @param articleId 文章id
     * @return
     */
    @Override
    public Result findCount(Long articleId) {
        Long count = commentMapper.selectCount(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getArticleId, articleId));
        return Result.OK(count);
    }

    /**
     * 新增评论
     * @param commentPutVo
     * @return
     */
    @Override
    public Result addComment(CommentPutVo commentPutVo) {
        // 内容校验
        if (StrUtil.isBlank(commentPutVo.getContent())){
            return Result.FILE("评论失败，不能为空！！");
        }
        // 违禁词查看
        List<ErrorKey> errorKeys = errorKeyMapper.selectList(null);
        for (ErrorKey errorKey : errorKeys) {
            if (commentPutVo.getContent().contains(errorKey.getText())){
                return Result.FILE("内容中不能有不健康词语！！");
            }
        }
        Comment comment = new Comment();
        BeanUtil.copyProperties(commentPutVo,comment);
        comment.setCreateTime(LocalDateTime.now());
        comment.setUserId(ThreadLocalUtil.getUid());
        comment.setCommentLikeCount(0L);

        commentMapper.insert(comment);
        return Result.OK(null);
    }

    // 递归得到评论的子评论
    private CommentDto recursion(CommentDto element,List<CommentDto> sources){
        for (CommentDto source : sources) {
            // 判断是否有子评论
            if (element.getCommentId().equals(source.getRootCommentId())){
                // 如果集合为空创建集合
                if (element.getCommentDtos() == null){
                    ArrayList<CommentDto> dtos = new ArrayList<>();
                    element.setCommentDtos(dtos);
                    // 将子评论加入父评论的子集合， resource() 再次递归获取子评论的子评论存入子评论的子集合
                    dtos.add(recursion(source,sources));
                }else {
                    element.getCommentDtos().add(recursion(source,sources));
                }
            }
        }
        return element;
    }
}
